import 'package:auto_route/auto_route.dart';
import 'package:casdon/app/config/theme/theme_extention.dart';
import 'package:casdon/common/state/common_net_status.dart';
import 'package:casdon/common/widget/common_app_bar.dart';
import 'package:casdon/common/widget/fullscreen_loading_overlay.dart';
import 'package:casdon/modules/me/application/notification/service_channel_list_notifier.dart';
import 'package:casdon/modules/me/application/notification/service_post_notifier.dart';
import 'package:casdon/modules/me/application/notification/service_product_list_notifier.dart';
import 'package:casdon/modules/me/domain/enums/service_order_category.dart';
import 'package:casdon/modules/me/ui/screen/service_installation_screen.dart';
import 'package:casdon/modules/me/ui/screen/service_repair_screen.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
@RoutePage()
class ServiceOrderPostScreen extends ConsumerStatefulWidget {
  const ServiceOrderPostScreen({
    super.key,
    this.initialCategory = ServiceOrderCategory.installation,
  });

  final ServiceOrderCategory initialCategory;

  @override
  ConsumerState<ServiceOrderPostScreen> createState() =>
      _ServiceOrderPostScreenState();
}

class _ServiceOrderPostScreenState extends ConsumerState<ServiceOrderPostScreen>
    with SingleTickerProviderStateMixin {
  late TabController _tabController;
  final List<String> tabs = ["预约安装", "预约维修"];

  @override
  void initState() {
    super.initState();
    final initialIndex =
        widget.initialCategory == ServiceOrderCategory.installation ? 0 : 1;
    _tabController = TabController(
      length: tabs.length,
      vsync: this,
      initialIndex: initialIndex,
    );
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // 在这里持有，有助于减少产品列表和渠道列表的获取次数。
    ref.watch(serviceChannelListProvider);
    ref.watch(serviceProductListProvider);
    final isInstallLoading = ref.watch(
      servicePostOrderNotifierProvider(
        installationServiceOrderParams,
      ).select((s) => s.status == CommonNetStatus.loading),
    );
    final isRepairLoading = ref.watch(
      servicePostOrderNotifierProvider(
        repairServiceOrderParams,
      ).select((s) => s.status == CommonNetStatus.loading),
    );
    final shouldShowLoading = isInstallLoading || isRepairLoading;
    return Stack(
      fit: StackFit.expand,
      children: [
        Scaffold(
          appBar: CommonAppBar(title: "报装报修"),
          body: Column(
            children: [
              Container(
                color: Colors.white,
                child: TabBar(
                  controller: _tabController,
                  tabs: tabs.map((tab) => Tab(text: tab)).toList(),
                  labelColor: context.colorScheme.primary,
                  labelStyle: context.textTheme.bodyLarge?.copyWith(
                    color: context.colorScheme.primary,
                  ),
                  indicatorPadding: const EdgeInsets.symmetric(horizontal: 16),
                  unselectedLabelColor:
                      context.customColorThemeNonNull.textColor1,
                  unselectedLabelStyle: context.textTheme.bodyMedium?.copyWith(
                    color: context.colorScheme.primary,
                  ),
                  indicatorColor: context.colorScheme.primary,
                  dividerColor: Colors.transparent,
                ),
              ),
              Expanded(
                child: TabBarView(
                  controller: _tabController,
                  children: tabs.map((tab) {
                    return _buildTabContent(tab);
                  }).toList(),
                ),
              ),
            ],
          ),
        ),
        if (shouldShowLoading) const FullscreenLoadingOverlay(),
      ],
    );
  }

  Widget _buildTabContent(String tabName) {
    switch (tabName) {
      case "预约安装":
        return const ServiceInstallationScreen();
      case "预约维修":
        return const ServiceRepairScreen(standalone: false);
      default:
        return Center(child: Text('$tabName 页面内容'));
    }
  }
}
